package com.onemileshop.service;

import com.onemileshop.entiy.PubConst;
import com.onemileshop.entiy.Session;
import com.onemileshop.utils.UtiLog;

import android.app.AlarmManager;
import android.app.PendingIntent;
import android.app.Service;
import android.content.Context;
import android.content.Intent;
import android.os.IBinder;
import android.os.SystemClock;
import android.util.Log;

public class OMSService extends Service
{
   @Override
   public IBinder onBind(Intent intent)
   {
	  // android.os.Debug.waitForDebugger();
	  return null;
   }

   @Override
   public int onStartCommand(Intent intent, int flags, int startId)
   {
	  if (Session.getInstance().IsDEBUG())
	  {
		 UtiLog.D("OMSService_onStart(intent, startId),服务开始 ");
	  }
	  return super.onStartCommand(intent, flags, startId);
   }

   @Override
   public void onCreate()
   {
	  try
	  {
		 super.onCreate();
		 // android.os.Debug.waitForDebugger();
		 if (Session.getInstance().IsDEBUG())
		 {
			UtiLog.D("OMSService_onCreate(),服务启动");
		 }
		 // 设置上传，每隔1分钟上传一次

		 Intent intent = new Intent(this, RevCheckNewOrder.class);
		 intent.setAction(PubConst.Action.CheckNewOrder);
		 PendingIntent pIntent = PendingIntent.getBroadcast(this, 0, intent, 0);
		 AlarmManager am2 = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
		 // 60秒一个周期，上传数据，系统休眠的时候也不会停止
		 am2.setRepeating(AlarmManager.ELAPSED_REALTIME_WAKEUP, SystemClock.elapsedRealtime(), PubConst.UPLOAD_TIME, pIntent);

		 if (Session.getInstance().IsDEBUG())
		 {
			UtiLog.D("OMSService_onCreate() ");
		 }
	  }
	  catch (Exception e)
	  {
		 if (Session.getInstance().IsDEBUG())
		 {
			UtiLog.E("OMSService_onCreate()" + e.toString());
		 }
		 Log.e("ERROR", "onCreate()", e);
	  }
   }

   @Override
   public void onStart(Intent intent, int startId)
   {
	  // android.os.Debug.waitForDebugger();
	  // super.onStart(intent, startId);
	  if (Session.getInstance().IsDEBUG())
	  {
		 UtiLog.D("YGLService_onStart(intent, startId),服务开始 ");
	  }
   }
}
